#if !defined(MYARRAY_H)
#define MYARRAY_H

#include <iostream>
using namespace std;

template <class T>
class myArray
{
private:
    T *array;
    int MAX;
    int num;
void FastSort(int begin,int end)
    {
        if(begin>=end)return;
        int mid =devide(begin,end,array);
        FastSort(begin,mid-1);
        FastSort(mid+1,end);
    }
 
 int devide(int begin,int end,T*arr)
    {
        T key=arr[begin];
        int low =begin;
        int high=end;
        while(low<high)
        {

            T temp;
            while((low<high)&&*(arr+high)>=key)
            {
                high--;
            }    
            if(low<high)
            {
               *(arr+low)=*(arr+high);
            }
            while((low<high&&*(arr+low)<=key))
            {
                low++;
            }
            if(low<high)
            {
                *(arr+high)=*(arr+low);
            }
        }
        *(arr+low)=key;
        return low;
    }

public: 
    myArray(int _length) : MAX(_length)
    {
        // array=(T *)malloc(sizeof(T) * _length) ;   //这里有两种方式，一种是这样 ，但是对于class将会无法操作 ，另一种如下
        array = new T[_length];
        num = 0; //好像和正常数组没啥区别了
    };
    ~myArray() { delete[] array; }
    T &operator[](int n)
    {
        if (n >= MAX)
        {
            throw exception();
        }
        return *(array + n);
    }
    void resize(int newSize)
    {
        if (newSize < MAX)
            throw exception();
        T* newArray = new T(newSize);
        for (int i = 0; i < MAX; i++)
        {
            newArray[i] = array[i];
        }
        array = newArray;
        delete[] array;
        MAX=newSize;
    }
    void push_back(T _ipt)
    {
        if ((num + 1) >= MAX)
        {
            resize(2 * MAX);
        }
        array[num]=_ipt;
        num++;
    }

          void sort()
    {
        FastSort(0,num-1);
    }
    void print()
    {
        for (int i =0;i<num;++i)
        {
            cout <<*(array+i)<<endl;
        }
    }
};

#endif // MYARRAY_H
